<?php
require_once('funcs.php');
require_once('Iterator.php');
class partilizer {
	public static function get_emp_arr($data , $fn_map){
		//var_dump($data);exit;

		if($data=='debug'){
			$data = file( dirname(__FILE__) . '/data.txt');
			$fn_map = null;
		}
		$it = new ArrayIterator2($data , 1 , $fn_map);
		$struct = array();
		$emp_arr = array();
		$wait_for_out = array();
		$last_index = array();
		$index = array();

		//$it->print_r();exit;
		$out_key = $fn_map['out-of-calc']['out'];
		$in_key  = $fn_map['out-of-calc']['in'];

		foreach($it as $k =>$v){
			$emp_code = $it->get_item('emp_code');
			//var_dump($emp_code)."\n";
			if(!isset($emp_arr[$emp_code])){
				$emp_arr[$emp_code] = new ArrayIterator2(array(),0,$fn_map);
				$wait_for_out[$emp_code] = false;
				$last_index[$emp_code] = 0;
				@$index[$emp_code] = -1;
			}
			if( true == $wait_for_out[$emp_code] && $it->get_item('fn') == $out_key ){
				$wait_for_out[$emp_code] = false;
				$emp_arr[$emp_code]->add('end_date_time' , calculate_date($it->get_item('date'),$it->get_item('time')) , $last_index[$emp_code]);
				$emp_arr[$emp_code]->add('end_timestamp' , calculate_timestamp($it->get_item('date'),$it->get_item('time')), $last_index[$emp_code]);
				$emp_arr[$emp_code]->add('trans_type' , globals::NON_CALCULATED_TRANSACTION ,$last_index[$emp_code]);//this already filled
				//$it2->addItem('start_date',$it->get_item('date'));
				//$deb_cont = true;
				continue;

			}
			if( false == $wait_for_out[$emp_code] && $it->get_item('fn') == $out_key ){

				//$minus_time = $emp_arr[$emp_code]->get_option('OUT', 'max_period');
				$minus_time = $emp_arr[$emp_code]->get_option_namspace('out-of-calc' , 'max_period');
				$previous_out_index = $it->search( 'fn', $out_key, $k-1 , true );
				if($previous_out_index >= 0){
					$prev_out_item = $it->get_item(null,$previous_out_index);
					$minus_time = $it->get_item('timestamp')-$prev_out_item['timestamp'] > $minus_time ? $minus_time : $it->get_item('timestamp')-$prev_out_item['timestamp']-10 ;
				}
				$start_date_time = calculate_date($it->get_item('date'),$it->get_item('time'),$minus_time,true);
				$start_timestamp = calculate_timestamp($it->get_item('date'),$it->get_item('time'),$minus_time,true);
				$index = $emp_arr[$emp_code]->add( 'start_date_time' , $start_date_time );
				$emp_arr[$emp_code]->add('start_timestamp' , $start_timestamp ,$index);
				$emp_arr[$emp_code]->add('trans_type' , globals::NO_CLOSEING_NON_CALCALUATED_TRANSACTION_START ,$index);


				$emp_arr[$emp_code]->add('end_date_time' , calculate_date($it->get_item('date'),$it->get_item('time')) , $index);
				$emp_arr[$emp_code]->add('end_timestamp' , calculate_timestamp($it->get_item('date'),$it->get_item('time'))  , $index);




				$wait_for_out[$emp_code] = false;
				continue;
			}
			if( false == $wait_for_out[$emp_code]  && $it->get_item('fn') == $in_key ){
				$index = $emp_arr[$emp_code]->add('start_date_time' , calculate_date($it->get_item('date'),$it->get_item('time')) );
				$emp_arr[$emp_code]->add('start_timestamp' , calculate_timestamp($it->get_item('date'),$it->get_item('time'))  , $index);
				$emp_arr[$emp_code]->add('trans_type' , globals::NON_CALCULATED_TRANSACTION ,$index);


				$wait_for_out[$emp_code] = true;
				$l_i = $emp_arr[$emp_code]->last_index();
				$last_index[$emp_code]  = $l_i;
				continue;
			}
			if( true == $wait_for_out[$emp_code]  && $it->get_item('fn') == $in_key ){
				$emp_arr[$emp_code]->add('end_date_time' , calculate_date($it->get_item('date'),$it->get_item('time'),1,TRUE) , $last_index[$emp_code]);
				$emp_arr[$emp_code]->add('end_timestamp' , calculate_timestamp($it->get_item('date'),$it->get_item('time'),1,TRUE), $last_index[$emp_code]);
				$emp_arr[$emp_code]->add('trans_type' , globals::NO_CLOSEING_NON_CALCALUATED_TRANSACTION_END ,$last_index[$emp_code]);

				$index = $emp_arr[$emp_code]->add('start_date_time' , calculate_date($it->get_item('date'),$it->get_item('time')) );
				$emp_arr[$emp_code]->add('start_timestamp' , calculate_timestamp($it->get_item('date'),$it->get_item('time')), $index);



				$wait_for_out[$emp_code] = true;
				continue;
			}
		}

		//print_r($emp_arr);exit;
		//$emp_arr['EMP1']->print_r();exit;

		foreach ($emp_arr as $emp_code => $it2) {
			foreach ( $it2 as $k =>$v ){
				$res_it = $it->search_range('timestamp', $it2->get_item('start_timestamp'), $it2->get_item('end_timestamp'));
				if($res_it){
					$leaves_array = array();
					foreach ($res_it as $kk => $vv){
						if($vv['fn']== $out_key || $vv['fn']== $in_key || $vv['emp_code'] != $emp_code) continue;
						$leaves_array[] = $vv;
					}
					$it2->add('leaves', new ArrayIterator2($leaves_array,0,$fn_map) , $k);
				}
			}
			//$struct[$emp_code] = clone $it2;

		}



		foreach ($emp_arr as $emp_code => $it2) {
			foreach ( $it2 as $k2 =>$v2 ){
				foreach ($it2->fn_map['leaves'] as $leave => $options){
					$in_key = $options['in'];
					$out_key = $options['out'];
					$leaves_item_it = $it2->get_item('leaves');
					if(!$leaves_item_it || $leaves_item_it->count() <= 0)continue;
					//echo "<pre>";print_r($leaves_item_it->array);continue;
					$orderd_leaves_it = $it2->get_item( 'orderd_leaves' );
					if(!$orderd_leaves_it){
						$orderd_leaves_it = new ArrayIterator2(array(),0,$fn_map);
					}
					$this_leave_it = add_leaves( $emp_code , $leaves_item_it , $leave , $out_key , $in_key ,$it2->get_item());
					//if(!$this_leave_it)continue;
					//echo "\n".$leave."\n";
					//$this_leave_it->print_r();
					if($this_leave_it){
						$orderd_leaves_it->add($leave,$this_leave_it);
					}
					$it2->add( 'orderd_leaves', $orderd_leaves_it, $k2 );
				}
				//$orderd_leaves_it->print_r();exit;
			}
		}
		
		return $emp_arr;

	}

}